package DataBase;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import Messages.MessageLoginReply.ErrMsg;
import Messages.MessageLoginReply.LoginStatus;
import DataObjects.User;
import com.mysql.jdbc.ResultSet;

 /**
 * This class checks if the user details are correct and give back the user details
 * @author Andrey
 * @author Igor
 *
 */
public class LoginCheck {
	String userName;
	String password;
	String ac_type;
	String name;
	int account_num;
	int id;
	private int stat;
	private int permission;
	private String eid;
	private LoginStatus status;
	private User loginUser;
	//private Permission permission; 
	private ErrMsg error;
	//int[] retStatus;
	SQLConnection sc;
	
	/**
	 * 
	 * @param user Username
	 * @param pass Password
	 */
	public LoginCheck(String user,String pass)
	{
		this.userName=user;
		this.password=pass;		
	}

	/**
	 * This method checks if the user details are correct and give back all it's details
	 */
	public void Check() 
	{
		try
		{		
		String user;
		String pass;
			
		sc=new SQLConnection();
		PreparedStatement ps1 = sc.getConnection().prepareStatement("SELECT username,password,permission FROM Login WHERE username=? and password=?;");
		PreparedStatement ps2 = sc.getConnection().prepareStatement("SELECT status,eid FROM Employee_Login WHERE username=?;");
		PreparedStatement ps3 = sc.getConnection().prepareStatement("SELECT name,phoneNo,role,hireDate,email,house,city,eid,street,lastName FROM Employee WHERE eid=?;");
		PreparedStatement ps4 = sc.getConnection().prepareStatement("UPDATE Employee_Login SET status=1 WHERE eid=?");
		 	
		//set userName and password in query
		ps1.setString(1, userName);
		ps1.setString(2, password);
		ps2.setString(1, userName);
		 		
		ResultSet rsUN=(ResultSet) ps1.executeQuery();			    				
		if (rsUN.next())
		{
			user=rsUN.getString(1);
			pass=rsUN.getString(2);
			permission=rsUN.getInt(3);
			
			ResultSet rsUN2=(ResultSet)ps2.executeQuery();
			if(rsUN2.next())
			{
				this.stat=rsUN2.getInt(1);
				this.eid=rsUN2.getString(2);
			}
			else
				this.error=ErrMsg.STATUS_PROBLEMM;
			rsUN2.close();
			ps2.close();
		}
		else
		{
			System.out.println("Unncorrect username or password");
			user="";
			pass="";			
		}
		rsUN.close();
		ps1.close();
	 
		
		if((userName.equals(user))&&(password.equals(pass)) )
		{			
		    if((stat==0))		//if userName and password is correct and the user is not logged in then Login the user
			{				
//				ps.setString(1, userName);
	//			ps.executeUpdate();
				this.status=LoginStatus.LOGGED_IN;				
			//	this.permission=ConvertPermissionToNum(rsUN.getString(3));
				this.error=ErrMsg.SUCCESS;
			 	ps3.setString(1, eid);
			 	ResultSet rsUN3=(ResultSet)ps3.executeQuery();
			 	if(rsUN3.next())
				{
			 		System.out.println("Correct LOGIN");
			 		loginUser=new User();
					loginUser.setName(rsUN3.getString(1));
					loginUser.setPhoneNo(rsUN3.getString(2));
					loginUser.setRole(rsUN3.getString(3));
					loginUser.setHireDate(rsUN3.getString(4));
					loginUser.setEmail(rsUN3.getString(5));
					loginUser.setHouse(rsUN3.getString(6));
					loginUser.setCity(rsUN3.getString(7));
					loginUser.setEid(rsUN3.getString(8));
					loginUser.setStreet(rsUN3.getString(9));
					loginUser.setLastName(rsUN3.getString(10));
					loginUser.setPermission(permission);
					ps4.setString(1, eid);
					ps4.executeUpdate();
					ps4.close();
					System.out.println("Update status success");
					
				}
				else
					this.error=ErrMsg.EmployeeId_PROBLEMM;
				rsUN3.close();
				ps3.close();
			}
		
			else if(stat==1)			//User is already logged in to the system
			{
				String per="NOT_RELEVANT";
				this.status=LoginStatus.LOGIN_FAIL;
				this.error=ErrMsg.ALREADY_LOGIN;
			}			
		}
			else			//userName or password are incorrect 
			{
				String per="NOT_RELEVANT";
				this.status=LoginStatus.LOGIN_FAIL;
				this.error=ErrMsg.INCORRECT;				 
			}
		
		
	
		}
	 catch (SQLException ex) 
 	{/* handle any errors*/
	System.out.println("SQLException: " + ex.getMessage());
	System.out.println("SQLState: " + ex.getSQLState());
	System.out.println("VendorError: " + ex.getErrorCode());	
    }
	
	}
	
	/**
	 * 
	 * @param per Permission type
	 * @return	Permission type
	 */
	
	/*
	private Permission convertPermissionToNum(String per)
	{
		 
		if(per.equals("Manager"))
			return Permission.MANAGER;
		if(per.equals("Teacher"))
			return Permission.TEACHER;
		if(per.equals("Student"))
			return Permission.STUDENT;
		
		return Permission.NOT_RELEVANT;
	}
	*/

	/**
	 * 
	 * @return Login Status
	 */
	public LoginStatus GetLoginStatus()
	{
		return status;
	}
	
	
	
	/**
	 * 
	 * @return Error Message
	 */
	public ErrMsg GetErrorMessage()
	{
		return error;
	}
	
	 
	
	/**
	 * 
	 * @return Username
	 */
	public User getLoginUser()
	{
		return this.loginUser;
	}
	
	
}
